package com.shushan.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * @ClassName: RoundProgressView
 * @Desciption: 圆圈进度控件
 * @author: yichaohua
 * @date: 2017-03-29
 */
public class RoundProgressView extends View {

    /**中心圆的颜色值*/
    private int mCenterRoundColor = Color.argb(255, 255, 255, 255);

    /**进度的颜色*/
    private int mProgressRoundColor = Color.argb(255, 255, 91, 72);
    /**进度的背景颜色*/
    private int mProgressRoundBgColor = Color.argb(255, 217, 217, 217);
    /**进度条的宽度*/
    private int mProgressWidth = 12;

    /***字体颜色*/
    private int mTextColor = Color.rgb(255, 91, 72);
    private float mPencentTextSize = 80;

    private int mWidth,mHeight;
    private int mPaddingX;

    private float mProgress = 0.0f;
    private float mMax = 1.0f;


    private Paint mPaint = new Paint();

    public RoundProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public RoundProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundProgressView(Context context) {
        super(context);
        init();
    }

    public void init(){
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();

        if(mWidth > mHeight){
            mWidth = mHeight;
        }
        mPaint.setAntiAlias(true); // 消除锯齿

        int halfWidth = mWidth/6;
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setColor(mProgressRoundBgColor);
        mPaint.setStyle(Paint.Style.STROKE);
        RectF oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*5+mPaddingX, halfWidth*5));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        mPaint.setColor(mProgressRoundColor);
        canvas.drawArc(oval, -90, 360*mProgress/mMax, false, mPaint);


        halfWidth = mWidth/5;
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mCenterRoundColor);
        oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*4+mPaddingX, halfWidth*4));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        mPaint.reset();
        mPaint.setTextSize(mPencentTextSize);
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextAlign(Paint.Align.CENTER);
        String number = String.format("%.0f", mProgress*100/mMax);
        canvas.drawText(number, mWidth/2+mPaddingX-25, mHeight/2+mPencentTextSize/3, mPaint);

        float textWidth = mPaint.measureText(number);
        mPaint.setTextSize(mPencentTextSize);
        canvas.drawText("%", mWidth/2+mPaddingX+textWidth/2+10, mHeight/2+mPencentTextSize/3, mPaint);

    }

    public void setMax(float mMax) {
        this.mMax = mMax;
    }

    public void setProgress(float mProgress) {
        this.mProgress = mProgress;
        invalidate();
    }

    public float getMax() {
        return mMax;
    }

    public float getProgress() {
        return mProgress;
    }
}
